<template>
  <div class="add-brand-container">
    <div class="header">
      <h2>{{ isEdit ? '编辑品牌' : '添加品牌' }}</h2>
      <button class="btn btn-secondary" @click="goBack">
        <i class="icon">←</i>
        返回
      </button>
    </div>

    <div class="form-container">
      <form @submit.prevent="submitForm" class="brand-form">
        <div class="form-row">
          <div class="form-group">
            <label for="name">品牌名称 *</label>
            <input
              type="text"
              id="name"
              v-model="form.name"
              placeholder="请输入品牌名称"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="country">国家/地区</label>
            <input
              type="text"
              id="country"
              v-model="form.country"
              placeholder="请输入国家或地区"
            />
          </div>
        </div>

        <div class="form-row">
          <div class="form-group">
            <label for="logo">品牌Logo URL</label>
            <input
              type="url"
              id="logo"
              v-model="form.logo"
              placeholder="请输入Logo图片链接"
            />
          </div>
          
          <div class="form-group">
            <label for="website">官方网站</label>
            <input
              type="url"
              id="website"
              v-model="form.website"
              placeholder="请输入官方网站地址"
            />
          </div>
        </div>

        <div class="form-row">
          <div class="form-group">
            <label for="foundedYear">成立时间</label>
            <input
              type="number"
              id="foundedYear"
              v-model="form.foundedYear"
              placeholder="请输入成立年份"
              min="1800"
              :max="new Date().getFullYear()"
            />
          </div>
          
          <div class="form-group">
            <label for="status">状态</label>
            <select id="status" v-model="form.status">
              <option value="active">活跃</option>
              <option value="inactive">停用</option>
            </select>
          </div>
        </div>

        <div class="form-group full-width">
          <label for="description">品牌描述</label>
          <textarea
            id="description"
            v-model="form.description"
            placeholder="请输入品牌描述"
            rows="4"
          ></textarea>
        </div>

        <div class="form-actions">
          <button type="button" class="btn btn-secondary" @click="goBack">
            取消
          </button>
          <button type="submit" class="btn btn-primary">
            {{ isEdit ? '更新品牌' : '创建品牌' }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AddBrand',
  props: {
    brandData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {
        name: '',
        logo: '',
        description: '',
        country: '',
        foundedYear: null,
        website: '',
        status: 'active'
      }
    };
  },
  computed: {
    isEdit() {
      return this.brandData !== null;
    }
  },
  watch: {
    brandData: {
      immediate: true,
      handler(newData) {
        if (newData) {
          this.form = {
            name: newData.name || '',
            logo: newData.logo || '',
            description: newData.description || '',
            country: newData.country || '',
            foundedYear: newData.foundedYear || null,
            website: newData.website || '',
            status: newData.status || 'active'
          };
        } else {
          this.resetForm();
        }
      }
    }
  },
  methods: {
    submitForm() {
      if (!this.validateForm()) {
        return;
      }

      // 模拟API调用
      setTimeout(() => {
        if (this.isEdit) {
          this.showToast('品牌更新成功！', 'success');
        } else {
          this.showToast('品牌创建成功！', 'success');
        }
        
        setTimeout(() => {
          this.goBack();
        }, 1000);
      }, 500);
    },

    validateForm() {
      if (!this.form.name.trim()) {
        this.showToast('请输入品牌名称', 'error');
        return false;
      }

      if (this.form.website && !this.isValidUrl(this.form.website)) {
        this.showToast('请输入有效的网站地址', 'error');
        return false;
      }

      if (this.form.logo && !this.isValidUrl(this.form.logo)) {
        this.showToast('请输入有效的Logo地址', 'error');
        return false;
      }

      return true;
    },

    isValidUrl(string) {
      try {
        new URL(string);
        return true;
      } catch (_) {
        return false;
      }
    },

    resetForm() {
      this.form = {
        name: '',
        logo: '',
        description: '',
        country: '',
        foundedYear: null,
        website: '',
        status: 'active'
      };
    },

    goBack() {
      this.$emit('navigate', '品牌管理');
    },

    showToast(message, type = 'info') {
      const toast = document.createElement('div');
      toast.className = `toast toast-${type}`;
      toast.textContent = message;
      
      Object.assign(toast.style, {
        position: 'fixed',
        top: '20px',
        right: '20px',
        padding: '12px 20px',
        borderRadius: '6px',
        color: 'white',
        fontSize: '14px',
        fontWeight: '500',
        zIndex: '10000',
        opacity: '0',
        transform: 'translateX(100%)',
        transition: 'all 0.3s ease',
        boxShadow: '0 4px 12px rgba(0, 0, 0, 0.2)'
      });

      const colors = {
        success: '#10b981',
        error: '#ef4444',
        warning: '#f59e0b',
        info: '#3b82f6'
      };
      
      toast.style.background = colors[type] || colors.info;
      document.body.appendChild(toast);
      
      setTimeout(() => {
        toast.style.opacity = '1';
        toast.style.transform = 'translateX(0)';
      }, 100);
      
      setTimeout(() => {
        toast.style.opacity = '0';
        toast.style.transform = 'translateX(100%)';
        setTimeout(() => {
          if (document.body.contains(toast)) {
            document.body.removeChild(toast);
          }
        }, 300);
      }, 3000);
    }
  }
};
</script>

<style scoped>
.add-brand-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #e5e7eb;
}

.header h2 {
  margin: 0;
  color: #1f2937;
  font-size: 24px;
  font-weight: 600;
}

.form-container {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.brand-form {
  max-width: 800px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.form-group label {
  margin-bottom: 8px;
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 12px 16px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.2s ease;
  background: white;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-group textarea {
  resize: vertical;
  min-height: 100px;
  font-family: inherit;
}

.form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e5e7eb;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}

.btn-primary {
  background: #3b82f6;
  color: white;
}

.btn-primary:hover {
  background: #2563eb;
  transform: translateY(-1px);
}

.btn-secondary {
  background: #6b7280;
  color: white;
}

.btn-secondary:hover {
  background: #4b5563;
  transform: translateY(-1px);
}

.icon {
  font-style: normal;
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .add-brand-container {
    padding: 15px;
  }
  
  .form-container {
    padding: 20px;
  }
  
  .form-row {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .form-actions {
    flex-direction: column-reverse;
  }
  
  .btn {
    width: 100%;
    justify-content: center;
  }
}
</style>